package gao.xiaolei.dao;


import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.LocalDateTime;
import java.util.List;

public interface CheckMethod<T> {
    public List<T> findByTargetId(String id);

    //根据审核对象来获取审核记录，仅获取id,isPass,checkMan,checkDate,并根据时间由最新的开始排序
    @Query("select id,isPass,checkMan,isFinish,checkDate from #{#entityName} where targetId=:targetId order by checkDate DESC")
    public List<Object[]> findByTarget(@Param("targetId") String targetId);

    //根据审核id来获取批注内容
    @Query("select annotate from #{#entityName} where id=:id")
    public String getAnnotate(@Param("id") String id);

    //根据审核id来获取是否通过审核
    @Query("select isPass from #{#entityName} where id=:id")
    public Integer getIsPass(@Param("id") String id);

    //获取审核时间
    @Query("select checkDate from #{#entityName} where id=:id")
    public LocalDateTime getCheckDate(@Param("id") String id);

    //根据审核id来获取审核对象Id
    @Query("select targetId from #{#entityName} where id=:id")
    public String getTarget(@Param("id") String id);

    //获取某个审核对象的审核次数
    @Query("select count(*) from #{#entityName} where targetId=:targetId")
    public Integer getCountByTargetId(@Param("targetId") String targetId);

    //修改审核的批注和结果
    @Modifying
    @Query("update #{#entityName} set annotate=:annotate,isPass=:isPass where id=:id")
    public void updateAnnotateAndIsPass(@Param("annotate") String annotate,
                                        @Param("isPass") Integer isPass,
                                        @Param("id") String id);

    //修改审核的结果
    @Modifying
    @Query("update #{#entityName} set isFinish=:isFinish where id=:id")
    public void updateIsFinish(@Param("isFinish") Integer isFinish, @Param("id") String id);

    //根据审核人以及是否通过审核来查找
    public List<T> findByCheckManAndIsFinishAndProjectId(String checkMan, Integer isFinish,String projectId);

    //根据审核id来获取是否通过审核
    @Query("select isFinish from #{#entityName} where id=:id")
    public Integer getIsFinish(@Param("id") String id);

    //获取比某个测试计划的审核的审核时间更新的审核的数量
    @Query("select count(*) from #{#entityName} where targetId=:targetId and checkDate>:checkDate")
    public Integer getCountByTargetIdAndCheckDateAfter(@Param("targetId") String targetId, @Param("checkDate") LocalDateTime checkDate);

    public void deleteAllByTargetIdIn(List<String> targetId);
}
